package com.jsgt.greekfire.ui.home.menu.pool

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.PoolBean
import com.jsgt.greekfire.viewmodel.PrivilegeViewModel
import kotlinx.android.synthetic.main.activity_pool.*
import kotlinx.android.synthetic.main.view_title.*
import org.jetbrains.anko.imageResource

class PoolActivity : BaseActivity() {
    private val privilegeViewModel: PrivilegeViewModel by lazy {
        ViewModelProvider(this).get(PrivilegeViewModel::class.java)
    }

    private val listAdapter: PoolListAdapter by lazy {
        PoolListAdapter(this, R.layout.item_pool_list)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_pool
    }

    override fun main() {
        title_tv.text = getString(R.string.pool)
        right_tv.visibility = View.VISIBLE
        right_tv.text = getString(R.string.pool_mine)

        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = listAdapter
        listAdapter.setOnItemClickListener(object:OnItemClickListener{
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                if (listAdapter.getItem(position).status == 1){
                    showToast(getString(R.string.pool_not_open))
                }else{
                    showToast(getString(R.string.pool_not_open))
                }
            }
        })

        privilegeViewModel.getPoolList()
        privilegeViewModel.poolListData.observe(this, Observer {
            listAdapter.setNewInstance(it as MutableList<PoolBean>)
        })
        privilegeViewModel.errorLiveData.observe(this, Observer {
            showToast(it.toString())
        })
    }

    class PoolListAdapter(context: Context, layoutResId: Int) :
        BaseQuickAdapter<PoolBean, BaseViewHolder>(layoutResId) {
        private val poolImageList by lazy {
            intArrayOf(
                R.mipmap.ic_pool_list1,
                R.mipmap.ic_pool_list2,
                R.mipmap.ic_pool_list3,
                R.mipmap.ic_pool_list4,
                R.mipmap.ic_pool_list5,
                R.mipmap.ic_pool_list6,
                R.mipmap.ic_pool_list7,
                R.mipmap.ic_pool_list8,
                R.mipmap.ic_pool_list9,
                R.mipmap.ic_pool_list10,
                R.mipmap.ic_pool_list11,
                R.mipmap.ic_pool_list12
            )
        }
        private val coinImageMap by lazy {
            mapOf(
                "1" to R.mipmap.main_asset_usdt_logo,
                "2" to R.mipmap.main_asset_met_logo,
                "3" to R.mipmap.main_asset_zwb_logo,
                "4" to R.mipmap.main_asset_ltb_logo,
                "5" to R.mipmap.main_asset_jjb_logo,
                "6" to R.mipmap.main_asset_fhb_logo,
                "7" to R.mipmap.main_asset_jft_logo
            )
        }

        override fun convert(holder: BaseViewHolder, item: PoolBean) {
            holder.setText(R.id.txvPool, item.poolName)
                .setImageResource(R.id.imvPool, poolImageList[item.guid - 1])
            val llCoins = holder.getView<LinearLayout>(R.id.llCoins)
            llCoins.removeAllViews()
            if (!item.assetTypeIn.isNullOrEmpty()) {
                val inArray = item.assetTypeIn!!.split(",")
                inArray.forEach {
                    if (coinImageMap.containsKey(it)) {
                        val view = LayoutInflater.from(context)
                            .inflate(R.layout.item_pool_list_coin, llCoins, false)
                        val imvCoin = view.findViewById<ImageView>(R.id.imvCoin)
                        imvCoin.imageResource = coinImageMap.getValue(it)
                        llCoins.addView(view)
                        if (inArray.indexOf(it) > 0) {
                            val layoutParams = view.layoutParams as LinearLayout.LayoutParams
                            layoutParams.marginStart = 10
                        }
                    }
                }
            }

            val llTransferCoins = holder.getView<LinearLayout>(R.id.llTransferCoins)
            llTransferCoins.removeAllViews()
            if (!item.assetTypeOut.isNullOrEmpty()) {
                val outArray = item.assetTypeOut!!.split(",")
                outArray.forEach {
                    if (coinImageMap.containsKey(it)) {
                        val view = LayoutInflater.from(context)
                            .inflate(R.layout.item_pool_list_coin, llTransferCoins, false)
                        val imvCoin = view.findViewById<ImageView>(R.id.imvCoin)
                        imvCoin.imageResource = coinImageMap.getValue(it)
                        llTransferCoins.addView(view)

                        if (outArray.indexOf(it) > 0) {
                            val layoutParams = view.layoutParams as LinearLayout.LayoutParams
                            layoutParams.marginStart = 10
                        }
                    }
                }
            }
        }
    }
}